package com.elitetyc.customview.custom;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.blankj.utilcode.util.ConvertUtils;
import com.elitetyc.customview.R;

/**
 * Created by elitetyc on 2019/4/4.
 * Describe:
 */
public class ZmxyBroadView extends View {
    private int maxNumber;
    private int startAngle;
    private int sweepAngle;
    private int sweepInWidth;
    private int sweepOutWidth;
    private Context mContext;
    private Paint paint,paint2,paint3,paint4;
    private int[] indicatorColor = {0xffffffff,0x00ffffff,0x99ffffff,0xffffffff};

    private int currentNum=100;

    private String text[] = {"较差","中等","良好","优秀","极好"};

    private int mWidth,mHeight;
//    圆的半径
    private int radius;
    public ZmxyBroadView(Context context) {
        this(context,null);
    }

    public ZmxyBroadView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ZmxyBroadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initAttr(attrs);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        设置防抖动
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(0xffffffff);
        paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint4 = new Paint(Paint.ANTI_ALIAS_FLAG);


    }

    private void initAttr(AttributeSet attrs) {
        TypedArray array = mContext.obtainStyledAttributes(attrs, R.styleable.ZmxyBoardView);
        maxNumber = array.getInt(R.styleable.ZmxyBoardView_maxNum,500);
        startAngle = array.getInt(R.styleable.ZmxyBoardView_startAngle,160);
        sweepAngle = array.getInt(R.styleable.ZmxyBoardView_sweepAngle,220);
        sweepInWidth = ConvertUtils.dp2px(8);
        sweepOutWidth = ConvertUtils.dp2px(3);
//        释放资源
        array.recycle();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int wSize = MeasureSpec.getSize(widthMeasureSpec);
        int wModel = MeasureSpec.getMode(widthMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);
        int hModel = MeasureSpec.getMode(heightMeasureSpec);

        if (wModel==MeasureSpec.EXACTLY){
//            说明是固定值或者wrap
            mWidth = wSize;
        }else {
//            如果是不受限制，默认给一个宽度
            mWidth = ConvertUtils.dp2px(300);
        }
        if (hModel == MeasureSpec.EXACTLY){
            mHeight = hSize;
        }else {
            mHeight = ConvertUtils.dp2px(200);
        }

//        最后将测量出来的宽高设置进去
        setMeasuredDimension(mWidth,mHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        radius = getMeasuredWidth()/4;
        canvas.save();
        canvas.translate(mWidth/2,mWidth/2);
//        画内外圆
        drawRound(canvas);
//        画刻度
        drawScale(canvas);
//        画当前进度值
        drawIndicator(canvas);
//        画中间的文字
        drawCenterText(canvas);
        canvas.restore();
    }

    private void drawCenterText(Canvas canvas) {
        canvas.save();
        paint4.setStyle(Paint.Style.FILL);
        paint4.setTextSize(radius/2);
        paint4.setColor(0xffffffff);
        canvas.drawText(currentNum+"",-paint4.measureText(currentNum+"")/2,0,paint4);
        paint4.setTextSize(radius/4);
        String content = "信用";
        if (currentNum<maxNumber*1/5){
            content +=text[0];
        }else if (currentNum>=maxNumber*1/5&&currentNum<maxNumber*2/5){
            content +=text[1];

        }else if (currentNum>=maxNumber*2/5&&currentNum<maxNumber*3/5){
            content +=text[2];

        }else if (currentNum>=maxNumber*3/5&&currentNum<maxNumber*4/5){
            content +=text[3];

        }else if (currentNum>=maxNumber*4/5){
            content +=text[4];
        }
        Rect rect = new Rect();
        paint4.getTextBounds(content,0,content.length(),rect);
        canvas.drawText(content,-rect.width()/2,rect.height()+20,paint4);
        canvas.restore();
    }

    private void drawIndicator(Canvas canvas) {
        canvas.save();
        paint2.setStyle(Paint.Style.STROKE);
        int sweep;
        if (currentNum<=maxNumber){
            sweep = (int)((float)currentNum/(float)maxNumber*sweepAngle);
        }else {
            sweep = sweepAngle;
        }
        paint2.setStrokeWidth(sweepOutWidth);
        Shader shader = new SweepGradient(0,0,indicatorColor,null);
        paint2.setShader(shader);
        int w = ConvertUtils.dp2px(10);
        RectF rectF = new RectF(-radius-w,-radius-w,radius+w,radius+w);
        canvas.drawArc(rectF,startAngle,sweep,false,paint2);

        float x = (float)((radius+ConvertUtils.dp2px(10))*Math.cos(Math.toRadians(startAngle+sweep)));
        float y = (float)((radius+ConvertUtils.dp2px(10))*Math.sin(Math.toRadians(startAngle+sweep)));
        paint3.setStyle(Paint.Style.FILL);
        paint3.setColor(0xffffffff);
        paint3.setMaskFilter(new BlurMaskFilter(ConvertUtils.dp2px(3),BlurMaskFilter.Blur.SOLID));
        canvas.drawCircle(x,y,ConvertUtils.dp2px(3),paint3);
        canvas.restore();
    }

    private void drawScale(Canvas canvas) {
        canvas.save();
        float angle =(float) sweepAngle/30;//刻度间隔
//        将刻度起始点旋转到最上方
        canvas.rotate(-270+startAngle);
        for (int i=0;i<=30;i++){
            if (i%6==0){
//                画刻度粗线
                paint.setStrokeWidth(ConvertUtils.dp2px(2));
                paint.setAlpha(0x70);
                canvas.drawLine(0,-radius-sweepInWidth/2,
                        0,-radius+sweepInWidth/2+ConvertUtils.dp2px(2)
                        ,paint);
                drawText(canvas,i*maxNumber/30+"",paint);
            }else {
//                画细刻度线
                paint.setStrokeWidth(ConvertUtils.dp2px(1));
                paint.setAlpha(0x50);
                canvas.drawLine(0,-radius-sweepInWidth/2,
                        0,-radius+sweepInWidth/2
                        ,paint);
            }

            if (i==3||i==9||i==15||i==21||i==27){
                paint.setStrokeWidth(ConvertUtils.dp2px(2));
                paint.setAlpha(0x50);
                drawText(canvas,text[(i-3)/6],paint);
            }
            canvas.rotate(angle);
        }
        canvas.restore();
    }

    private void drawText(Canvas canvas, String s, Paint paint) {
        paint.setStyle(Paint.Style.FILL);
        paint.setTextSize(ConvertUtils.sp2px(8));
        float width = paint.measureText(s);
        canvas.drawText(s,-width/2,-radius+ConvertUtils.dp2px(15),paint);
        paint.setStyle(Paint.Style.STROKE);

    }

    private void drawRound(Canvas canvas) {
        canvas.save();
//        画内园
        paint.setAlpha(0x40);
        paint.setStrokeWidth(sweepInWidth);
        RectF rectF = new RectF(-radius,-radius,radius,radius);
        canvas.drawArc(rectF,startAngle,sweepAngle,false,paint);
//        外圆
        paint.setStrokeWidth(sweepOutWidth);
        int w = ConvertUtils.dp2px(10);
        RectF rectF1 = new RectF(-radius-w,-radius-w,radius+w,radius+w);
        canvas.drawArc(rectF1,startAngle,sweepAngle,false,paint);
        canvas.restore();
    }


    public int getCurrentNum() {
        return currentNum;
    }

    public void setCurrentNum(int currentNum) {
        this.currentNum = currentNum;
//        设置数值的时候重新绘界面
        invalidate();
    }

    public void setCurrentNumberAnim(int num){
        float duration = (float) Math.abs(num-currentNum)/maxNumber*1500+500;
        ObjectAnimator animator = ObjectAnimator.ofInt(this,"currentNum",num);
        animator.setDuration((long)Math.min(duration,2000));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                int color  = calculateColor(value);
                setBackgroundColor(color);
            }
        });
        animator.start();
    }

    private int calculateColor(int value) {
        ArgbEvaluator evaluator = new ArgbEvaluator();
        float fraction = 0;
        int color = 0;
        if (value<=maxNumber/2){
            fraction = (float)value/(maxNumber/2);
//            由红色到橙色
            color = (int)evaluator.evaluate(fraction,0xffff6347,0xffff8c00);
        }else {
            fraction = ((float)value-maxNumber/2)/(maxNumber/2);
//            由橙色到蓝色
            color = (int) evaluator.evaluate(fraction,0xffff8c00,0xff00ced1);
        }
        return color;
    }
}
